package model;

import java.util.Date;

import org.mybeans.dao.DAOException;
import org.mybeans.factory.BeanFactory;
import org.mybeans.factory.BeanFactoryException;
import org.mybeans.factory.BeanTable;
import org.mybeans.factory.MatchArg;
import org.mybeans.factory.RollbackException;
import org.mybeans.factory.Transaction;

import databeans.*;

public class PaymentDAO {
    //FactoryBean objects participate in the containing BeanFactory's synchronization of bean creation. 
	private BeanFactory<Payment> factory;
	
	public PaymentDAO() throws DAOException {
		try {
	        // Get a BeanTable to create the "position" table
	        BeanTable<Payment> paymentTable = BeanTable.getInstance(Payment.class,"cfa_check_transaction");
	        
	        if (!paymentTable.exists()){
	        	paymentTable.create("paymentID");
	        	
	        	paymentTable.setIdleConnectionCleanup(true);
	        	factory = paymentTable.getFactory();
	        	bootData();
	        }else {
	        
	        // Long running web apps need to clean up idle database connections.
	        // So we can tell each BeanTable to clean them up. 
	        paymentTable.setIdleConnectionCleanup(true);
	
	        // Get a BeanFactory which the actions will use to read and write
	        // rows of the "position" table
	        factory = paymentTable.getFactory();
	        }
		} catch (BeanFactoryException e) {
			throw new DAOException(e);
		}
	}
	
    private void bootData() throws DAOException {
        Date d = new Date();
        
        Payment payment = new Payment();
        payment.setAmount(34566);
        payment.setCustomerID(1);
        payment.setExecuted(true);
        payment.setTransactionType(databeans.Transaction.CHECK_DEPOSIT);  
        d.getTime();
        payment.setSubmitDate(d); 
        
        create(payment);
        
        payment = new Payment();
        payment.setAmount(60066);
        payment.setCustomerID(1);
        payment.setExecuted(false);
        payment.setTransactionType(databeans.Transaction.CHECK_REQUEST);
        d.getTime();
        payment.setSubmitDate(d); 
        
        create(payment);
        
        payment = new Payment();
        payment.setAmount(6266);
        payment.setCustomerID(2);
        payment.setExecuted(true);
        payment.setTransactionType(databeans.Transaction.CHECK_DEPOSIT);
        d.getTime();
        payment.setSubmitDate(d); 
        
        create(payment);
        
        payment = new Payment();
        payment.setAmount(63366);
        payment.setCustomerID(3);
        payment.setExecuted(true);
        payment.setTransactionType(databeans.Transaction.CHECK_DEPOSIT);
        d.getTime();
        payment.setSubmitDate(d); 
        
        create(payment);
        
        
    }

	public void create(Payment payment) throws DAOException {
		try {
            Transaction.begin();
            databeans.Payment dbPayment = factory.create(); //no need to input anything since the pk is generated automatically
            
            factory.copyInto(payment, dbPayment);
            Transaction.commit();
		} catch (RollbackException e) {
			throw new DAOException(e);
		} finally {
			if (Transaction.isActive()) Transaction.rollback();
		}
	}

	public void delete(int paymentID) throws DAOException {
		try {
			Transaction.begin();
    		Payment payment = factory.lookup(paymentID);

    		if (payment == null) {
				throw new DAOException("Payment does not exist: paymentID = "+paymentID);
    		}

    		if (paymentID!= payment.getPaymentID()) {
				throw new DAOException("Payment not found by paymentID "+paymentID);
    		}

			factory.delete(payment);
			org.mybeans.factory.Transaction.commit();
		} catch (RollbackException e) {
			throw new DAOException(e);
		} finally {
			if (Transaction.isActive()) Transaction.rollback();
		}
	}
	
	public Payment[] getPayments(int customerID) throws DAOException {
		try {
			Payment[] list = factory.match(MatchArg.equals("customerID", customerID));
			//Payment[] list = factory.match(MatchArg.equals("customerID",customerID));
			return list;
		} catch (RollbackException e) {
			throw new DAOException(e);
		}
	}
	
	/*
   public Payment[] getPendingPayments(int customerID) throws DAOException {
        try {
            Payment[] list = factory.match(MatchArg.equals("customerID",customerID), MatchArg.equals("executed","0"));
            return list;
        } catch (RollbackException e) {
            throw new DAOException(e);
        }
    }
	    
<<<<<<< .mine
	*/

	   
	   public Payment[] getAllPendingDepositPayments() throws DAOException {
	        try {
	            Payment[] list = factory.match(MatchArg.equals("transactionType",databeans.Transaction.CHECK_DEPOSIT), MatchArg.equals("executed",false));
	            return list;
	        } catch (RollbackException e) {
	            throw new DAOException(e);
	        }
	    }
	   
	   public Payment[] getAllPendingWithdrawalPayments() throws DAOException {
	        try {
	        	Payment[] list = factory.match(MatchArg.equals("transactionType",databeans.Transaction.CHECK_REQUEST), MatchArg.equals("executed",false));
	            return list;
	        } catch (RollbackException e) {
	            throw new DAOException(e);
	        }
	    }
	   

	    public void updateToApproved(Payment p) throws DAOException {
	        try {
	            Transaction.begin();
	            int id = p.getPaymentID();

	            Payment dbPayment = factory.lookup(id);
	        
	            if (dbPayment == null) {
	                throw new DAOException("Payment does not exist paymentID= " + id);
	            }
	            
	            dbPayment.setExecuted(true);
	            Transaction.commit();
	        } catch (RollbackException e) {
	            throw new DAOException(e);
	        } finally {
	            if (Transaction.isActive())
	                Transaction.rollback();
	        }
	    }
	

	public Payment lookup(int paymentID) throws DAOException {
		try {
			return factory.lookup(paymentID);
		} catch (RollbackException e) {
			throw new DAOException(e);
		}
	}
	
}
      
